package com.yeyks.commonReference.service.vip.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yeyks.common.base.Arguments;
import com.yeyks.common.em.vip.VipCommissionBillOperatorTypeEnum;
import com.yeyks.common.em.vip.VipCommissionBillTypeEnum;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.commonReference.service.vip.VipCommissionBillService;
import com.yeyks.commonReference.service.vip.VipCommissionOverviewService;
import com.yeyks.vip.dal.domain.VipCommissionBill;
import com.yeyks.vip.dal.domain.VipCommissionOverview;
import com.yeyks.vip.dal.dao.VipCommissionOverviewMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


/**
 * <p>
 * vip订单分佣总览表 服务实现类
 * </p>
 *
 * @author yun
 * @since 2019-08-21
 */
@Slf4j
@Service
public class VipCommissionOverviewServiceImpl extends ServiceImpl<VipCommissionOverviewMapper, VipCommissionOverview> implements VipCommissionOverviewService {
    @Autowired
    private VipCommissionBillService vipCommissionBillService;

    /**
     * 通过分佣账户id获取vip分佣账户
     *
     * @param id
     * @return
     */
    @Override
    public VipCommissionOverview selectByAccountId(Integer id) {
        Arguments.intIsNull(id, "分佣账户id不能为空");
        VipCommissionOverview overview = baseMapper.selectOne(new QueryWrapper<VipCommissionOverview>().eq(VipCommissionOverview.ACCOUNT_ID, id));
        if (null == overview) {
            return getVipCommissionOverview(id);
        }
        return overview;
    }

    /**
     * vip佣金提现拒绝或失败余额回退
     *
     * @param orderNo
     * @param remark
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean remainAdd(String orderNo, String remark) {
        log.info("vip佣金提现拒绝或失败余额回退的订单号为orderNo = {}", orderNo);
        Arguments.strIsNull(orderNo, "提现订单号为空");
        VipCommissionBill bill = vipCommissionBillService.getOne(new QueryWrapper<VipCommissionBill>()
                .eq(VipCommissionBill.ORDER_NO, orderNo)
                .eq(VipCommissionBill.OPERATOR_TYPE, VipCommissionBillOperatorTypeEnum.OUT.value())
                .eq(VipCommissionBill.REMARK, VipCommissionBillOperatorTypeEnum.OUT.desc()));
        Arguments.beanIsNull(bill, "该提现订单明细不存在");
        Date date = new Date();
        VipCommissionOverview overview = selectByAccountId(bill.getAccountId());
        overview.setTotalOutAmount(overview.getTotalOutAmount() - bill.getOperatorAmount());
        overview.setRemainAmount(overview.getRemainAmount() + bill.getOperatorAmount());
        overview.setUpdateTime(date);
        Arguments.isTrue(baseMapper.updateById(overview) != 1, "修改vip佣金账户表异常");
        //写入账户明细表信息
        vipCommissionBillService.insertBill(date, bill.getAccountId(), null,null,
                VipCommissionBillOperatorTypeEnum.IN.value(),
                VipCommissionBillOperatorTypeEnum.IN.desc(), bill.getOperatorAmount(), bill.getOrderNo());
        return true;
    }

    private VipCommissionOverview getVipCommissionOverview(Integer accountId) {
        VipCommissionOverview overview = new VipCommissionOverview();
        overview.setAccountId(accountId);
        overview.setTotalInAmount(0);
        overview.setTotalOutAmount(0);
        overview.setRemainAmount(0);
        Date date = new Date();
        overview.setCreateTime(date);
        overview.setUpdateTime(date);
        baseMapper.insert(overview);
        return overview;
    }
}
